package com.cx.bank.view;

import com.cx.bank.dao.BankDaoInterface;
import com.cx.bank.factory.UserDaoFactory;
import com.cx.bank.handler.MyInVocationHandler;
import com.cx.bank.manager.IManager;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.pojo.User;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author jxau_scf
 * @date 2021/7/19 - 20:07
 */
public class TransferMoneyView extends JFrame {
    private JButton confirm;
    private JLabel moneyLabel, idLabel;
    public JTextField moneyText, idText;


    public TransferMoneyView(){
        building();
        addlisteners();
    }

    private void building() {
        setLayout(null);
        moneyLabel = new JLabel("转账金额");
        idLabel = new JLabel("转账用户");
        confirm = new JButton("确认");
        moneyText = new JTextField(20);
        idText = new JTextField(20);

        confirm.setBounds(250, 500, 120, 25);
        idLabel.setBounds(150, 200, 120, 25);
        idText.setBounds(250, 200, 200, 30);
        moneyLabel.setBounds(150, 250, 120, 25);
        moneyText.setBounds(250, 250, 200, 30);

        add(confirm);
        add(idLabel);
        add(idText);
        add(moneyLabel);
        add(moneyText);

        setBounds(600,600,600,600);
        setLocation((2000-getWidth())/2,(1000-getHeight())/2);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private void addlisteners() {
        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String toUserName = idText.getText();
                Double transferMoney = Double.valueOf(moneyText.getText());
                try {
                    UserDaoFactory userDaoFactory = UserDaoFactory.getInstance();
                    BankDaoInterface bankDao = userDaoFactory.getBankDao();
                    ManagerImpl manager = ManagerImpl.getInstance();
                    manager.setBankDao(bankDao);
                    User user = BankView.getUser();

                    MyInVocationHandler handler = new MyInVocationHandler();
                    handler.setTarget(manager);
                    handler.setUserName(user.getUserName());
                    IManager proxy = (IManager)handler.getProxy();

//                    boolean transferStatus = manager.transfer(user.getUserName(), toUserName, transferMoney);
                    boolean transferStatus = proxy.transfer(user.getUserName(), toUserName, transferMoney);
                    if (transferStatus){
                        //关闭当前页面
                        TransferMoneyView.this.dispose();
                    }else {
                        System.out.println("转账失败");
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                } catch (InstantiationException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }

                //关闭当前页面
                TransferMoneyView.this.dispose();

            }
        });
    }

    public static void main(String[] args) {
        TransferMoneyView transferMoneyView = new TransferMoneyView();
        transferMoneyView.show();
    }
}
